﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using SoftwareLogistics.SharepointClient.Library;
using SoftwareLogistics.SharepointClient.Library.Models;

namespace SoftwareLogistics.SharepointClient.Silverlight.Controls
{
    public class EditField : Grid, IEditField
    {
        private TextBox _textBox;
        private CheckBox _checkBox;
        private TextBlock _label;
        private ComboBox _listPicker;
        private DatePicker _datePicker;


        public EditField()
        {
            _textBox = new TextBox { Width = 300 };
            _label = new TextBlock();
            _checkBox = new CheckBox();
            _listPicker = new ComboBox() { Width = 300 };
            _datePicker = new DatePicker { Width = 300 };

            Children.Add(_checkBox);
            Children.Add(_textBox);
            Children.Add(_label);
            Children.Add(_listPicker);
            Children.Add(_datePicker);
        }

        SPField _field;
        public SPField Field
        {
            get{return _field;}
            set
            {
                _field = value;
                _field.Editor = this;
                SetValue();
            }
        }

        private void SetValue()
        {
            _textBox.Visibility = Visibility.Collapsed;
            _label.Visibility = Visibility.Collapsed;
            _listPicker.Visibility = Visibility.Collapsed;
            _datePicker.Visibility = Visibility.Collapsed;
            _checkBox.Visibility = Visibility.Collapsed;

            switch (Field.FieldType)
            {
                case FieldTypes.Note:                    
                    _textBox.Text = Field.Value ?? "";
                    _textBox.Height = 80;
                    _textBox.Visibility = Visibility.Visible;
                    _textBox.TextWrapping = TextWrapping.Wrap;
                    _textBox.AcceptsReturn = true;

                    break;
                case FieldTypes.Text:
                    _textBox.Text = Field.Value ?? "";
                    _textBox.Visibility = Visibility.Visible;
                    _textBox.TextWrapping = TextWrapping.NoWrap;
                    break;
                case FieldTypes.Number:
                    _textBox.Text = Field.GetFormattedNumber();
                    _textBox.Visibility = Visibility.Visible;
                    break;
                case FieldTypes.Lookup:
                    _label.Text = Field.GetFormattedLookupValue();
                    _label.Visibility = Visibility.Visible;
                    break;
                case FieldTypes.Choice:
                    _listPicker.ItemsSource = Field.Choices;
                    if (!string.IsNullOrEmpty(Field.Value))
                        _listPicker.SelectedItem = Field.Value;

                    _listPicker.Visibility = Visibility.Visible;
                    break;
                case FieldTypes.Boolean:
                    _checkBox.IsChecked = Field.GetIsChecked();
                    _checkBox.Visibility = Visibility.Visible;
                    break;
                case FieldTypes.DateTime:
                    _datePicker.SelectedDate = Field.GetDateTime();
                    _datePicker.Visibility = Visibility.Visible;
                    break;
                case FieldTypes.User:
                    _listPicker.ItemsSource = Common.Users.Users;
                    _listPicker.DisplayMemberPath = "Name";
                    var user = Field.GetUser();
                    if (user != null)
                        _listPicker.SelectedItem = user;
                    _listPicker.Visibility = Visibility.Visible;

                    break;
            }
        }

        public void RefreshValue()
        {
            switch (Field.FieldType)
            {
                case FieldTypes.Number:
                    Field.Value = String.IsNullOrEmpty(_textBox.Text) ? string.Empty : _textBox.Text;
                    break;
                case FieldTypes.Text:
                case FieldTypes.Note:
                    Field.Value = String.IsNullOrEmpty(_textBox.Text) ? string.Empty : _textBox.Text;
                    break;
                case FieldTypes.Lookup: break;
                case FieldTypes.Choice:
                    Field.Value = _listPicker.SelectedItem == null ? string.Empty : _listPicker.SelectedItem.ToString();
                    break;
                case FieldTypes.DateTime:
                    Field.Value = Common.GetSPDateFormat(_datePicker.SelectedDate);
                    break;
                case FieldTypes.Boolean:
                    if (Field.Required || (_checkBox.IsChecked.HasValue && _checkBox.IsChecked.Value))
                        Field.Value = _checkBox.IsChecked.ToString().ToUpper();

                    break;
                case FieldTypes.User:
                    var user = _listPicker.SelectedItem as SPUser;
                    if (user != null)
                        Field.Value = string.Format("{0};#{1}", user.ID, user.Name);
                    break;
            }
        }
    }
}
